package com.diyunapp.xilinapp.fragments;


import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;
import android.widget.Toast;

import com.diyunapp.xilinapp.R;
import com.diyunapp.xilinapp.adapters.WaitPayAdapter;
import com.diyunapp.xilinapp.bean.WaitPayBean;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest;
import com.nuttysoft.nutand.views.pullview.PullToRefreshView;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * A simple {@link Fragment} subclass.
 */
public class WaitPayFragment extends Fragment {


    @Bind(R.id.waitpay_list)
    ListView waitpayList;
    @Bind(R.id.waitpay_refresh_view)
    PullToRefreshView waitpayRefreshView;
    private WaitPayAdapter waitPayAdapter;
    private List<WaitPayBean.DatasBean.OrderGroupListBean.OrderListBean> info = new ArrayList<>();
    private List<WaitPayBean.DatasBean.OrderGroupListBean.OrderListBean.ExtendOrderGoodsBean> info1=new ArrayList<>();
    private LayoutInflater inflater;


    private Context context;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
//        return inflater.inflate(R.layout.fragment_wait_pay, container, false);
        super.onCreateView(inflater, container, savedInstanceState);
        this.inflater = inflater;
        View view = inflater.inflate(R.layout.fragment_wait_pay, null);
        ButterKnife.bind(this, view);
        context = getActivity();
        ButterKnife.bind(this, view);
        waitPayAdapter = new WaitPayAdapter(getContext());
//        getData("http://121.42.195.167:82/huibaointerface/touziguanliapp.php");
//        getInvesting();
        waitpayList.setAdapter(waitPayAdapter);
        return view;
    }

    public void getInvesting() {

        String url = "http://www.xilingou.com/mobile/index.php?act=member_order&op=order_list" +
                "&key=fbcdca5b1216edd0bec677aa41584452&type=daifukuan";
        HttpUtils http = new HttpUtils();
        http.configCurrentHttpCacheExpiry(1000);
        http.send(HttpRequest.HttpMethod.GET,url,
                new RequestCallBack<String>()
                {
                    @Override
                    public void onLoading(long total, long current, boolean isUploading)
                    {

                    }

                    @Override
                    public void onSuccess(ResponseInfo<String> responseInfo)
                    {
//                        bidFreshLv.onRefreshComplete();
                        String resultinfo = responseInfo.result;
//                        Log.i("zhangxiaoxidebug", "resultinfo="+resultinfo);
                        try {
                            JSONObject resultObject = new JSONObject(resultinfo);
                            int result = resultObject.getInt("code");
//                            Log.i("zhangxiaoxidebug", "code="+result);
                            if(result==200)
                            {
                                JSONObject resultObject1 = resultObject.optJSONObject("datas");
//                                Log.i("zhangxiaoxidebug", "recordObject="+resultObject1);
                                JSONArray listArray = resultObject1.optJSONArray("order_group_list");
                                for(int i=0;i<listArray.length();i++)
                                {
                                    JSONArray listArray1 = resultObject1.optJSONArray("order_list");
                                    for(int k=0;k<listArray1.length();k++)
                                    {
                                        JSONObject recordObject = listArray1.optJSONObject(k);

                                    String order_id = recordObject.optString("order_id");
                                    String order_sn = recordObject.optString("order_sn");
                                    String store_name = recordObject.optString("store_name");
                                    String order_state=recordObject.optString("order_state");
                                    JSONArray extend_order_goods = recordObject.optJSONArray("extend_order_goods");
                                    Log.i("zhangxiaoxidebug", "recordObject="+extend_order_goods);
                                    for(int j=0;j<extend_order_goods.length();j++){
                                        JSONObject recordObject1 = extend_order_goods.optJSONObject(j);
                                        Log.i("zhangxiaoxidebug", "recordObject1="+recordObject1);
                                        String goods_name=recordObject1.optString("goods_name");
                                        String goods_num=recordObject1.optString("goods_num");
                                        String goods_price=recordObject1.optString("goods_price");
                                        String goods_pay_price=recordObject1.optString("goods_pay_price");
                                        info1.add(new WaitPayBean.DatasBean.OrderGroupListBean.OrderListBean.ExtendOrderGoodsBean(
                                                goods_name,goods_num,goods_price,goods_pay_price
                                        ));
//                                        WaitPayBean.DatasBean.OrderGroupListBean.OrderListBean.ExtendOrderGoodsBean extend_order_goods=info1;

                                    }

                                        info.add(new WaitPayBean.DatasBean.OrderGroupListBean.OrderListBean
                                            (order_id, order_sn, store_name,order_state,extend_order_goods));
                                }}
                                waitPayAdapter.setList(info);

                            }
                            else
                            {
                                waitpayRefreshView.setVisibility(View.GONE);
                                Toast.makeText(getActivity(),"您还没有相关投资记录！", Toast.LENGTH_SHORT).show();
                            }
                        } catch (JSONException e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                            waitpayRefreshView.setVisibility(View.GONE);
                            Toast.makeText(getActivity(),"查询失败，请联系客服处理！",Toast.LENGTH_SHORT).show();

                        }
                    }

                    @Override
                    public void onFailure(HttpException error, String msg)
                    {
                        waitpayRefreshView.setVisibility(View.GONE);
                        Toast.makeText(getActivity(),"查询失败，请检查您的网络！",Toast.LENGTH_SHORT).show();

                    }
                });
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.unbind(this);
    }


}




